
    /**  
    * @Title: LogStorageMysqlService.java
    * @Package com.efast.cafe.commponent.log.service.impl
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年8月31日
    * @version V1.0  
    */
    
package com.efast.cafe.commponent.log.service.impl;

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import com.efast.cafe.commponent.log.bean.LogInfoQueryBean;
import com.efast.cafe.commponent.log.constant.LogConstant;
import com.efast.cafe.commponent.log.entity.BusLogInfo;
import com.efast.cafe.commponent.log.service.LogStorageService;
import com.efast.cafe.framework.bean.QueryPageBase;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.persistence.Criteria;
import com.efast.cafe.framework.pager.Pagination;
import com.efast.cafe.util.ConfigUtil;
import com.efast.cafe.util.StringUtil;
import com.efast.cafe.util.date.DateUtils;

/**
    * @ClassName: LogStorageMysqlService
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @author Administrator
    * @date 2018年8月31日
    *
    */
@ConditionalOnProperty(
        name=LogConstant.K_STORAGE,
        havingValue="mysql"
)
@Service("logStorageMysqlService")
@Transactional("mysqlLogDruidDataSource")
public class LogStorageMysqlServiceImpl  implements LogStorageService{
	
	private static final Integer expireDay = Integer.parseInt(ConfigUtil.getValue(LogConstant.K_LOG_EXPIRE_DAY));
	
	/**
	CREATE TABLE `bus_log_info` (
	  `bus_log_info_id` int(11) NOT NULL AUTO_INCREMENT,
	  `user_name` varchar(50) DEFAULT NULL,
	  `level` varchar(50) DEFAULT NULL,
	  `system_no` varchar(50) DEFAULT NULL,
	  `system_version` varchar(50) DEFAULT NULL,
	  `company_code` varchar(50) DEFAULT NULL,
	  `msg` text,
	  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
	  `remark` varchar(300) DEFAULT NULL,
	  `client_ip` varchar(50) DEFAULT NULL,
	  `id` varchar(50) DEFAULT NULL,
	  `service_name` varchar(100) DEFAULT NULL,
	  PRIMARY KEY (`bus_log_info_id`),
	  KEY `id` (`id`)
	) ENGINE=InnoDB AUTO_INCREMENT=26821 DEFAULT CHARSET=utf8;
	**/
	

	@Autowired
	@Qualifier("mysqlLogJdbcDao")
	private JdbcDao jdbcDao;
	
	
	
	    /* (非 Javadoc)
	    * 
	    * 
	    * @param logInfoBean
	    * @see com.efast.cafe.commponent.log.service.LogStorageService#saveLog(com.efast.cafe.commponent.log.entity.LogInfo)
	    */
	@Override
	public void saveLog(BusLogInfo logInfoBean) {
		Criteria criteria = Criteria.create(BusLogInfo.class)
				.set("userName", logInfoBean.getUserName())
				.set("level", logInfoBean.getLevel())
				.set("systemNo", logInfoBean.getSystemNo())
				.set("systemVersion", logInfoBean.getSystemVersion())
				.set("companyCode", logInfoBean.getCompanyCode())
				.set("msg", logInfoBean.getMsg())
				.set("createTime", DateUtils.parse(logInfoBean.getCreateTime(), DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS))
				.set("remark", logInfoBean.getRemark())
				.set("id", logInfoBean.getId())
				.set("serviceName", logInfoBean.getServiceName())
				.set("category", logInfoBean.getCategory())
				.set("clientIp", logInfoBean.getClientIp());
		
				jdbcDao.insert(criteria);
	}

	
	    /* (非 Javadoc)
	    * 
	    * 
	    * @param logInfoQueryBean
	    * @return
	    * @see com.efast.cafe.commponent.log.service.LogStorageService#queryPage(com.efast.cafe.commponent.log.bean.LogInfoQueryBean)
	    */
	    
	@Override
	public QueryPageBase queryPage(LogInfoQueryBean logInfoQueryBean) {
		Assert.notNull(logInfoQueryBean.getSystemNo(), "SystemNo Can't be empty");;
		Pagination<BusLogInfo> pager = new Pagination<>();
		Criteria criteria = Criteria.create(BusLogInfo.class);
		int page = logInfoQueryBean.getStart()==0?1:(logInfoQueryBean.getStart()/logInfoQueryBean.getLength())+1;
		pager.setCurrentPage(page);
		pager.setNumPerPage(logInfoQueryBean.getLength());
		criteria.where("systemNo", new Object[]{logInfoQueryBean.getSystemNo()});
		if(StringUtil.isNotBlank(logInfoQueryBean.getUserName())){
			criteria.and("userName", new Object[]{logInfoQueryBean.getUserName()});
		}
		if(StringUtil.isNotBlank(logInfoQueryBean.getSystemVersion())){
			criteria.and("systemVersion", new Object[]{logInfoQueryBean.getSystemVersion()});
		}
		if(StringUtil.isNotBlank(logInfoQueryBean.getLevel())){
			criteria.and("level", new Object[]{logInfoQueryBean.getLevel()});
		}
		if(StringUtil.isNotBlank(logInfoQueryBean.getCompanyCode())){
			criteria.and("companyCode", new Object[]{logInfoQueryBean.getCompanyCode()});
		}
		if(StringUtil.isNotBlank(logInfoQueryBean.getMsg())){
			criteria.and("msg",Criteria.FIELD_OPERATOR_LIKE, new Object[]{"%"+logInfoQueryBean.getMsg()+"%"});
		}
		if(StringUtil.isNotBlank(logInfoQueryBean.getRemark())){
			criteria.and("remark",Criteria.FIELD_OPERATOR_LIKE, new Object[]{"%"+logInfoQueryBean.getRemark()+"%"});
		}
		if(StringUtil.isNotBlank(logInfoQueryBean.getCategory())){
			criteria.and("category",Criteria.FIELD_OPERATOR_LIKE, new Object[]{"%"+logInfoQueryBean.getCategory()+"%"});
		}
		String[] times = logInfoQueryBean.getCreateTime();
		if(times!=null){
			if(times.length==2){
				String startTimeStr = times[0];//start time
				if(!StringUtil.isEmpty(startTimeStr)){
					Date startTime = DateUtils.parse(startTimeStr, DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS);
					criteria.and("createTime", Criteria.FIELD_OPERATOR_GE,new Object[]{startTime});
				}
				String endTimeStr = times[1];
				if(!StringUtil.isEmpty(endTimeStr)){
					Date endTime = DateUtils.parse(endTimeStr, DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS);
					criteria.and("createTime", Criteria.FIELD_OPERATOR_LE,new Object[]{endTime});
				}
			}
			
		}
		pager = jdbcDao.queryListByPage(pager, criteria);
		logInfoQueryBean.setTotalRows(pager.getTotalRows());
		logInfoQueryBean.setResultList(pager.getResultList());
		return logInfoQueryBean;
	}


	

}
